<?php
// +----------------------------------------------------------------------
// | Description: 1
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-24 15:37:11
// +----------------------------------------------------------------------

namespace app\client\model;

use app\common\model\CommonClient;
use think\Cache;

class Product extends CommonClient
{
    protected $fuzzyConditions = ['ProductName', 'ProductCode', 'Title', 'MobbileDescription', 'ShortDescription', 'Description'];
    protected $clearConditions = ['CategoryId', 'OnSale'];
    protected $returnFields = '
        shop_goods_products.*,
        shop_goods_categories.CategoryName,
        shop_goods_brand.BrandName
    ';
    protected $rangeConditions = null;
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = [
        'shop_goods_categories'  => ['Id', 'CategoryId'],
        'shop_goods_brand'  => ['Id', 'BrandId'],
    ];
    protected $name = 'shop_goods_products';

     /**
     * 解析搜索条件
     */
    public function getDataList()
    {
        $this->page = $this->param['page'] or 1;
        $this->limit = $this->param['limit'] or $this->limit;
        $this->orderByString = $this->param['orderByString'];

        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();

        if ($this->limit == -1) {
            $resData = $this->field($this->returnFields)->select();
        } else {
            $resData = $this->field($this->returnFields)->page($this->page, $this->limit)->select();
        }
// p($this -> getlastsql());
        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where('IsDelete', 0);

        $dataCount = $this->count();
        $data['list'] = $resData;
        $data['dataCount'] = $dataCount;
        return $this->filter($data);
    }

    /**
     * 解析搜索条件
     */
    public function getDataListWithOutDetail()
    {
        $this->returnFields = '
        shop_goods_products.*,
        shop_goods_products.CategoryId,
        shop_goods_products.ShowSaleCounts,
        shop_goods_products.SortBy,
        shop_goods_products.MarketPrice,
        shop_goods_products.CurrentPrice,
        shop_goods_products.SeriesId,
        shop_goods_products.BrandId,
        shop_goods_products.ImgPath,
        shop_goods_products.VisitiCounts,
        shop_goods_products.AddedDate,
        shop_goods_products.Unit,
        shop_goods_products.ProductName,
        shop_goods_products.ProductCode,
        shop_goods_products.Title,
        shop_goods_products.One,
        shop_goods_products.Tow,
        shop_goods_products.Three,
        shop_goods_products.Five,
        shop_goods_products.Four,
        shop_goods_categories.CategoryName,
        shop_goods_brand.BrandName
    ';
        $this->page = $this->param['page'] or 1;
        $this->limit = $this->param['limit'] or $this->limit;
        $this->orderByString = $this->param['orderByString'];

        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();

        if ($this->limit == -1) {
            $resData = $this->field($this->returnFields)->select();
        } else {
            $resData = $this->field($this->returnFields)->page($this->page, $this->limit)->select();
        }
// p($this -> getlastsql());
        $this->specialCondition();
        $this->M_fuzzy();
        $this->M_clear();
        $this->M_range();
        $this->M_union_field();
        $this->M_UnionSelect();
        $this->M_order();
        // $this->where('IsDelete', 0);

        $dataCount = $this->count();
        $data['list'] = $resData;
        $data['dataCount'] = $dataCount;
        return $this->filterWithOutDetail($data);
    }

    public function filterWithOutDetail($data)
    {
        foreach($data['list'] as $k => $v){
            unset($data['list'][$k]['MobbileDescription']);
        }
        return $data;
    }


    /*
    * [getDataById 根据主键获取详情]
    * @linchuangbin
    * @DateTime  2017-02-10T21:16:34+0800
    * @param     string                   $id [主键]
    * @return    [array]
    */
   public function getDataById($id = '')
   {
       $data = $this->get($id);
       if (!$data) {
           $this->error = '暂无此数据';
           return false;
       }

       if(Cache::get('ProductId_'.$id, false)){
            $data['MobbileDescription'] = Cache::get('ProductId_'.$id);
        }

       return $data;
   }

    public function filter($data){
        foreach($data['list'] as $k => $v){
            if(Cache::get('ProductId_'.$v['Id'], false)){
                $data['list'][$k]['MobbileDescription'] = Cache::get('ProductId_'.$v['Id']);
            }
        }
        return $data;
    }


    /**
     * 通过id修改用户
     * @param  array   $param  [description]
     */
    public function updateDataById($param, $id, $fields = true, $pk = 'Id')
    {
        Cache::set('ProductId_'.$id, $param['MobbileDescription']);
        unset($param['MobbileDescription']);// 改用缓存存储数据
        try {
            $this->allowField($fields)->save($param, [$pk => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '编辑失败';
            return false;
        }
    }
}
